package ReflectTest;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/*
通过反射机制来创建一个对象
 */
public class ReflectConstructorTest2 {
    public static void main(String[] args) {
        try {
            Class c = Class.forName("java.lang.String");
            //调用无参构造方法
            c.newInstance();
            char[] chars = {'我','是','中','国','人'};
            //拿到构造方法
            Constructor constructor = c.getDeclaredConstructor(char[].class);
            //调用有参构造方法，传入参数
            Object o = constructor.newInstance(chars);
            System.out.println(o);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
